# !/usr/bin/env python
# coding: utf8


'''
Created on 28 mai 2009

@author: tmeedendorp
'''
import pygtk
pygtk.require('2.0')
import gtk
from plugin.category.thematic_tree import ThematicsTree
from plugin.category.thematic_tree import CategoriesTree
from plugin.category.thematic_tree import FiltersTree

from manager.bean_store      import *

class ConfigureThematicWindow(gtk.Dialog):
    '''
    classdocs
    '''
    def addEvent(self, widget, donnees=None):
        pass
        
    def editEvent(self, widget, donnees=None):
        pass
        
    def deleteEvent(self, widget, donnees=None):
        pass
        
    def apply(self, widget, donnees=None):
        getBean("configurationManager").saveConfiguration()
        pass
    
    def upItem(self, widget, donnees=None):
        tuple1, treeIter = self.tree.get_selection().get_selected_rows()
        if treeIter == None:
            return
        print treeIter.get_iter_first()
        pass
    
    def downItem(self, widget, donnees=None):
        pass
    
    def cancel(self, widget, donnees=None):
        self.destroy()
        
    def categoryChanged(self, selection):
        model, iter = selection.get_selected()
        if iter != None:
            value = model.get_value(iter,0)
            self.filtersTree.set_model(model.filtersTreeDic[value].get_model())
               
    def thematicChanged(self, selection):
        model, iter = selection.get_selected()
        if iter != None:
            value = model.get_value(iter,0)
            self.categoriesTree.set_model(model.categoriesTreeDic[value].get_model())
            iterFirstPath = self.categoriesTree.get_model().get_iter_first()
            self.categoriesTree.get_selection().select_iter(iterFirstPath)
        
    def __init__(self, parent):
        '''
        Constructor
        '''
        gtk.Dialog.__init__(self, title="Config", parent=parent, flags=0, buttons=None)
        self.set_border_width(10)
        self.set_title("Configuration des thématiques")
        self.set_default_size(400, 300)
        
        #thematic tree
        self.thematicsTree = ThematicsTree(getBean("configurationManager").getThematicList())
        self.thematicsTree.show()
        selection = self.thematicsTree.get_selection() 
        selection.connect('changed', self.thematicChanged)


        #category tree
        self.categoriesTree = CategoriesTree([])
        self.categoriesTree.show()
        selection = self.categoriesTree.get_selection() 
        selection.connect('changed', self.categoryChanged)
        
        #filters tree
        self.filtersTree = FiltersTree([])
        self.filtersTree.show()
        
        
        #buttons
        self.addButton = gtk.Button("Ajouter")
        self.editButton = gtk.Button("Éditer")
        self.deleteButton = gtk.Button("Supprimer")
        self.saveButton = gtk.Button("Appliquer")
        self.cancelButton = gtk.Button("Annuler")
        self.upButton = gtk.Button("Monter")
        self.downButton = gtk.Button("Descendre")
        
        self.addButton.connect("clicked", self.addEvent)
        self.editButton.connect("clicked", self.editEvent)
        self.deleteButton.connect("clicked", self.deleteEvent)
        self.saveButton.connect("clicked", self.apply)
        self.cancelButton.connect("clicked", self.cancel)
        self.upButton.connect("clicked", self.upItem)
        self.downButton.connect("clicked", self.downItem)
        self.addButton.show()
        self.editButton.show()
        self.deleteButton.show()
        self.saveButton.show()
        self.cancelButton.show()
        self.upButton.show()
        self.downButton.show()
        
        #boxes
        listBox = gtk.HBox(False, 0)
        listButtonsBox = gtk.VBox(False, 0)
        saveCancelBox = gtk.HBox(False, 0)
        
        listBox.show()
        listButtonsBox.show()
        saveCancelBox.show()
        self.show()
        
        listButtonsBox.pack_start(self.addButton, False, False, 0)  
        listButtonsBox.pack_start(self.editButton, False, False, 0)  
        listButtonsBox.pack_start(self.deleteButton, False, False, 0) 
        listButtonsBox.pack_start(self.upButton, False, False, 0)  
        listButtonsBox.pack_start(self.downButton, False, False, 0) 
        listBox.pack_start(self.thematicsTree, True, True, 0)   
        listBox.pack_start(self.categoriesTree, True, True, 0)   
        listBox.pack_start(self.filtersTree, True, True, 0)   
        listBox.pack_start(listButtonsBox, False, False, 10)  
        saveCancelBox.pack_start(self.saveButton, True, True, 0)  
        saveCancelBox.pack_start(self.cancelButton, True, True, 0)  
        
        self.vbox.pack_start(listBox, True, True, 0)  
        self.vbox.pack_start(saveCancelBox, False, False, 0)
        
        iterFirstPath = self.thematicsTree.get_model().get_iter_first()
        self.thematicsTree.get_selection().select_iter(iterFirstPath)